package com.genius.reservation.receiver.controller;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.genius.reservation.car.entity.ReservationStartCarInformation;
import com.genius.reservation.common.ResponseRule;
import com.genius.reservation.mapper.ReceiveRecordInformationMapper;
import com.genius.reservation.mapper.ReservationStartCarInformationMapper;
import com.genius.reservation.receiveRecord.entity.ReceiveRecordInformation;
import com.genius.reservation.receiver.service.IReceiverInformationService;
import com.genius.reservation.reservation.entity.ReservationInformation;
import com.genius.reservation.reservation.entity.ReservationOrderInformation;
import com.genius.reservation.reservation.service.IReservationInformationService;
import com.genius.reservation.tools.annotation.webLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author baomidou
 * @since 2022-05-26
 */
@RestController
@RequestMapping("/reservation/receiver")
@Api(tags = "收货员控制器", value = "收货员控制器")

public class ReceiverInformationController {

    @Autowired
    IReceiverInformationService receiverService;
    @Autowired
    IReservationInformationService reservationService;
    @Autowired
    @Lazy
    ReservationStartCarInformationMapper startCarMapper;
    @Autowired
    @Lazy
    ReceiveRecordInformationMapper recordMapper;

    @PostMapping("/getInspectItem/{reservationNumber}")
    @ApiOperation("开始预检")
    ResponseRule startInspect(
            @ApiParam("预约单编号") @PathVariable("reservationNumber") String reservationNumber
    ) {
        return receiverService.getInspectItem(reservationNumber);
    }

    @PostMapping("/getAllReceiver")
    @ApiOperation("获取所有的收货员")
    ResponseRule getAllReceiver(){
        return ResponseRule.ok(receiverService.list());
    }

    @PostMapping("/receiverHoliday")
    @ApiOperation("收货员休假")
    ResponseRule receiverHoliday(String reservationNumber){
        return receiverService.receiverHoliday(reservationNumber);
    }

    @PostMapping("/receiverWork")
    @ApiOperation("收货员上班")
    ResponseRule receiverWork(String reservationNumber){
        return receiverService.receiverWork(reservationNumber);
    }

    @PostMapping("/inspectSuccess/{reservationNumber}")
    @ApiOperation("预检完成")
    ResponseRule startInspect(@ApiParam("收货员编号") @RequestParam String receiverNumber
            , @ApiParam("预约单编号") @PathVariable("reservationNumber") String reservationNumber
            , @ApiParam("预检编号") @RequestParam Integer inspectNumber
    ) {
        return receiverService.inspectSuccess(reservationNumber, receiverNumber, inspectNumber);
    }

    @PostMapping("/getAllInspectByReservation/{reservationNumber}")
    @ApiOperation("获取所有预检信息")
    ResponseRule getAllInspectByReservation(@ApiParam("收货员编号和预约单编号") @RequestBody JSONObject body, @PathVariable("reservationNumber") String reservationNumber
    ) {
        String receiverNumber = body.getString("receiverNumber");
        System.out.println(receiverNumber);
        System.out.println(reservationNumber);
        return receiverService.getAllInspectByReservation(reservationNumber, receiverNumber);
    }

    @PostMapping("/delInspect/{reservationNumber}")
    @ApiOperation("删除一条预检信息")
    ResponseRule delInspect(@ApiParam("收货员编号") @RequestParam String receiverNumber
            , @ApiParam("预约单编号") @PathVariable("reservationNumber") String reservationNumber
            , @ApiParam("预检编号") @RequestParam Integer inspectNumber
    ) {
        return receiverService.delInspect(reservationNumber, receiverNumber, inspectNumber);
    }

    @PostMapping("/startReceive/{reservationNumber}")
    @ApiOperation("开始收货")
    ResponseRule startReceive(
            @ApiParam("预约单编号") @PathVariable("reservationNumber") String reservationNumber,
            @RequestBody JSONObject body
    ) {
        String receiverNumber = body.getString("receiverNumber");
        String carCode = body.getString("carCode");
        return receiverService.startReceive(reservationNumber, carCode, receiverNumber);
    }


    @PostMapping("/getReceiveSpeed")
    @ApiOperation("获取收货面板详情")
    @webLog
    ResponseRule getReceiveSpeed(
            @RequestBody JSONObject body
    ) {
        String startCarId = body.getString("startCarId");
        ReservationStartCarInformation carInformation = startCarMapper.selectById(startCarId);
        ReservationInformation reservationServiceByReservationNumber = reservationService.getByReservationNumber(carInformation.getReservationNumber());
        LambdaQueryWrapper<ReceiveRecordInformation> eq = recordMapper.getWrapper().eq(ReceiveRecordInformation::getReceiveRecordNumber, carInformation.getReceiveRecordNumber());
        ReceiveRecordInformation receiveRecordInformation = recordMapper.selectOne(eq);
        if (ObjectUtil.isEmpty(receiveRecordInformation)) {
            return ResponseRule.no("未找到记录");
        }
        List<String> list = new ArrayList<>();
        for (ReservationOrderInformation order : reservationServiceByReservationNumber.getOrders()) {
            list.add(order.getOrderNumber());
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("OrderID", list);

        //获取所有订单的收货效率
        HttpResponse execute = HttpRequest.post("http://39.107.83.199:8045/api/EditGoodsClass/ReceivedAmount").body(jsonObject.toJSONString()).execute();



        try {
            //获取件数
            receiveRecordInformation.setTotalCount(reservationServiceByReservationNumber.getGdsNumber());
            //设置收货数
            receiveRecordInformation.setReceiveCount(Integer.valueOf(execute.body())<=0?0:Integer.valueOf(execute.body()));

            //收获数量÷总数量×100=收货率
            receiveRecordInformation.setReceivePercentage((float) receiveRecordInformation.getReceiveCount() / receiveRecordInformation.getTotalCount() * 100);

            //获取收货开始时间
            String receiveStartTime = receiveRecordInformation.getReceiveStartTime();
            //获取计算确认结束时间
            String s = DateUtil.parse(receiveStartTime).offset(DateField.MINUTE, Integer.parseInt(reservationServiceByReservationNumber.getConfirmOverTime())).toString();

            //设置完成时间
            receiveRecordInformation.setReceiveCompleteTime(s);
            ReceiveRecordInformation up = recordMapper.up(receiveRecordInformation);


            HashMap<String, Object> objectObjectHashMap = new HashMap<>();
            objectObjectHashMap.put("startCar", carInformation);
            objectObjectHashMap.put("reservation", reservationServiceByReservationNumber);
            objectObjectHashMap.put("record", up);
            return ResponseRule.ok(objectObjectHashMap);

        } catch (Exception e) {
            e.getStackTrace();
            return ResponseRule.no("服务器错误,获取收货数值不正确");
        }
    }


    @PostMapping("/getReceiveFinish")
    @ApiOperation("获取收货完成面板详情")
    @webLog
    ResponseRule getReceiveFinish(
            @RequestBody JSONObject body
    ) {
        String startCarId = body.getString("startCarId");
        ReservationStartCarInformation carInformation = startCarMapper.selectById(startCarId);
        ReservationInformation reservationServiceByReservationNumber = reservationService.getByReservationNumber(carInformation.getReservationNumber());
        LambdaQueryWrapper<ReceiveRecordInformation> eq = recordMapper.getWrapper().eq(ReceiveRecordInformation::getReceiveRecordNumber, carInformation.getReceiveRecordNumber());
        ReceiveRecordInformation receiveRecordInformation = recordMapper.selectOne(eq);
        if (ObjectUtil.isEmpty(receiveRecordInformation)) {
            return ResponseRule.no("未找到记录");
        }
        HashMap<String, Object> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("startCar", carInformation);
        objectObjectHashMap.put("reservation", reservationServiceByReservationNumber);
        objectObjectHashMap.put("record", receiveRecordInformation);
        return ResponseRule.ok(objectObjectHashMap);
    }


    @PostMapping("/finishReceive/{reservationNumber}")
    @ApiOperation("完成收货")
    ResponseRule finishReceive(
            @ApiParam("预约单编号") @PathVariable("reservationNumber") String reservationNumber, @RequestBody JSONObject body
    ) {
        String receiverNumber = body.getString("receiverNumber");
        String receiveRecordNumber = body.getString("receiveRecordNumber");
        return receiverService.finishReceive(reservationNumber, receiverNumber, receiveRecordNumber);
    }
//    @GetMapping("/getAllMessage/{receiverNumber}")
//    @ApiOperation("获取所有消息")
//    ResponseRule getAllMessage(@PathVariable("receiverNumber") String receiverNumber){
//        return receiverService.getAllMessage(receiverNumber);
//    }

    @PostMapping("/getAllReservation")
    @ApiOperation("获取所有预约单")
    ResponseRule getAllReservation(@RequestParam String receiverNumber) {
        return receiverService.findStartCarByReceiver(receiverNumber);
    }

//    @PostMapping("/getAllReservationFive")
//    @ApiOperation("获取五个预约单")
//    ResponseRule getAllReservationFive(@RequestParam String receiverNumber){
//        return  reservationService.findReservationByReceiverFive(receiverNumber);
//    }

    @PostMapping("/getIncompleteReservationByReceiver")
    @ApiOperation("获取未完成订单")
    ResponseRule getIncompleteReservationByReceiver(@RequestParam String receiverNumber) {
        return reservationService.getIncompleteReservationByReceiver(receiverNumber);
    }

    @PostMapping("/getIncompleteReservationByReceiverByStartCar")
    @ApiOperation("获取发车信息里的未完成订单")
    ResponseRule getIncompleteReservationByReceiverByStartCar(
            @RequestBody JSONObject body
    ) {
        String receiverNumber = body.getString("receiverNumber");
        return reservationService.getIncompleteReservationByReceiverByStartCar(receiverNumber);
    }


    @PostMapping("/getStartCarInfo")
    @ApiOperation("获取发车信息里的完成订单")
    ResponseRule getStartCarInfo(
            @RequestBody JSONObject body
    ) {
        Integer startCarId = body.getInteger("startCarId");
        ReservationStartCarInformation finishListByStartCarId = startCarMapper.getFinishListByStartCarId(startCarId);
        if (ObjectUtil.isEmpty(finishListByStartCarId)) {
            return ResponseRule.no("未找到车辆");
        }
        return ResponseRule.ok(finishListByStartCarId);
    }

    @PostMapping("/getStatistical")
    @ApiOperation("获取本月业绩看板")
    @webLog
    ResponseRule getStatistical(
            @RequestBody JSONObject body
    ) {
        String receiverNumber = body.getString("receiverNumber");
//        Integer startCarId = body.getInteger("startCarId");
//        ReservationStartCarInformation finishListByStartCarId = startCarMapper.getFinishListByStartCarId(startCarId);
//        if (ObjectUtil.isEmpty(finishListByStartCarId)) {
//            return ResponseRule.no("未找到车辆");
//        }
        return receiverService.getStatistical(receiverNumber);
    }

//    @PostMapping("/confirmReservation")
//    @ApiOperation("预约单确认")
//    ResponseRule confirmReservation(@RequestParam String receiverNumber,@RequestParam String reservationNumber){
//        return  receiverService.confirmReservation(receiverNumber,reservationNumber);
//    }


}
